package com.bomberdroid.controller;

import java.util.Date;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.Log;

import com.bomberdroid.R;
import com.bomberdroid.model.Player;
import com.bomberdroid.protocols.DataSharing;
import com.bomberdroid.ui.GamePanel;

public class GameBombeButton {

	private static final String TAG = GameBombeButton.class.getSimpleName();

	private Player player;
	private int indicePlayer;

	private Bitmap _bombeButton;
	private Bitmap _bombeButtonBg;
	private Bitmap _bombeButtonBgActif;

	private boolean actif;

	private int bombeButtonHalfSize;
	private int bombeButtonBgHalfSize;

	private Point origButtonPoint;

	private int seuilButton = 100;

	public GameBombeButton(Resources res, Player p, int width, int height) {

		Log.i(TAG, "Constructeur " + TAG);
		_bombeButton = (Bitmap) BitmapFactory.decodeResource(res, R.drawable.bombe_button);
		_bombeButtonBg = (Bitmap) BitmapFactory.decodeResource(res, R.drawable.joystick_bg);
		_bombeButtonBgActif = (Bitmap) BitmapFactory.decodeResource(res, R.drawable.joystick_bg_actif);

		bombeButtonHalfSize = (int) _bombeButton.getHeight() / 2;
		bombeButtonBgHalfSize = (int) _bombeButtonBg.getHeight() / 2;

		actif = false;
		
		player = p;
		
		origButtonPoint = new Point(width-(int)(bombeButtonBgHalfSize*1.6), height-(int)(bombeButtonBgHalfSize*1.6));

	}

	public int getBombeButtonSize() {
		return bombeButtonHalfSize;
	}

	public int getBombeButtonBgSize() {
		return bombeButtonBgHalfSize;
	}

	public Point getOrigine() {
		return origButtonPoint;
	}

	public int getSeuil() {
		return seuilButton;
	}

	public Bitmap get_bombeButton() {
		return _bombeButton;
	}

	public Bitmap get_bombeButtonBg() {
		if (actif)
			return _bombeButtonBgActif;
		else
			return _bombeButtonBg;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	public void setIndicePlayer(int indicePlayer) {
		this.indicePlayer = indicePlayer;
	}

	void onTouchBombeButton(Point touch) {
		
		if (!actif){
			if(GamePanel.server){
				player.setControl(Player.POSER, new Date());
			}
			else{
				DataSharing.message += "3;"+indicePlayer+"&";
			}
		}
		
		actif = true;
	
	}

	void inactif() {
		
		if (actif){
			if(GamePanel.server){
				player.setControl(Player.POSER, null);
			}
			else{
				DataSharing.message += "4;"+indicePlayer+"&";
			}
			
		}
		actif = false;

	}

	boolean getEtat() {
		return actif;
	}

	public void onDraw(Canvas canvas) {

		canvas.drawBitmap(get_bombeButtonBg(), origButtonPoint.x - bombeButtonBgHalfSize, origButtonPoint.y
				- bombeButtonBgHalfSize, null);

		canvas.drawBitmap(_bombeButton, origButtonPoint.x - bombeButtonHalfSize, origButtonPoint.y - bombeButtonHalfSize, null);
	}

}
